<?php
error_reporting(E_ERROR);
function retrievePost($uri, $params){
  $param;  
  foreach($params as $key=>$value){
    $param  .= $key.'='.$value.'&';
  }
  
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $uri);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}

//print $_POST['j'];
//Retrieve PML in RDF/XML format
$path = $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
$paths = explode('/', $path);
array_pop($paths);
$serviceUri = implode('/', $paths).'/exporter.php';
$param = array('j' => urlencode($_POST['j']));
$rdfxml = retrievePost($serviceUri, $param);
//print $rdfxml;
//Validate data
$serviceUri = 'http://onto.rpi.edu/iw2api/pmlvalidator';
$param2 = array('output' => urlencode('xml'),
                'inputTextRdfSyntax' => urlencode('RX'),
                'inputText' => urlencode($rdfxml));

$result = retrievePost($serviceUri, $param2);
$xml = simplexml_load_string($result);
//print_r($xml);

$json = array();
$json['result'] = (string)$xml->result;
$report = array();
$counter=0;
foreach ($xml->hasReport as $r) {
  if(isset($r->OieReport->hasEntry)){
    $report["$counter"]= array('level' => (string)$r->OieReport->hasEntry->OieEntry->level, 'details' => html_entity_decode((string)$r->OieReport->hasEntry->OieEntry->message));
    $counter++;
  }
}
$json['report']= $report;
echo json_encode($json);
?>
